package com.douwong.student.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.douwong.student.R;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.model.Features;
import com.douwong.student.utils.CyptoUtils;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 导读详情适配器
 * @author leihuajie
 *
 */
public class FeatureDetailAdapter extends BaseAdapter{

	private ArrayList<Features> list;
	private Context context;
	public FeatureDetailAdapter(Context context, ArrayList<Features>list){
		this.context=context;
		this.list=list;
	}
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder=null;
		if(convertView==null){
			holder=new Holder();
			convertView=LayoutInflater.from(context).inflate(R.layout.features_detail_item, null);
			holder.title_text=(TextView) convertView.findViewById(R.id.title_text);
			holder.content_text=(TextView) convertView.findViewById(R.id.content_text);
			convertView.setTag(holder);
		}else {
			holder=(Holder) convertView.getTag();
		}
		if(list.size()>0){
			holder.title_text.setText(list.get(position).getTitle());
			holder.content_text.setMovementMethod(LinkMovementMethod.getInstance());
			String formatContent=handleHtmlStr(list.get(position).getContent().trim());
			holder.content_text.setText(getClickableHtml(formatContent));
//			holder.content_text.setText(Html.fromHtml(list.get(position).getContent().trim()));
		}
		return convertView;
	}
	private String handleHtmlStr(String html){
		try {
			if(html.contains("microclassdetail")){
				String useKey= BaseApplication.sUserBean.getUserbean().getEuNumber()+"|"+ CyptoUtils.MD5("123456");
				String pageUrl="href=http://www.eclassedu.com/edu/index/login.html?pageCode=9";
				String microId=null;
				Pattern pattern=Pattern.compile("href=\"(.+?)\"");
				Matcher matcher=pattern.matcher(html);
				while(matcher.find()){
					String tag=matcher.group();
					microId=tag.substring(tag.lastIndexOf("/")+1, tag.lastIndexOf("."));
					StringBuilder sBuilder=new StringBuilder();
					sBuilder.append(pageUrl);
					sBuilder.append("&microid=");
					sBuilder.append(microId);
					sBuilder.append("&key=");
					sBuilder.append(Base64.encodeToString(useKey.getBytes("utf-8"),Base64.DEFAULT));
					html=matcher.replaceFirst(sBuilder.toString());
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return html.trim();
	}

	private void setLinkClickable(final SpannableStringBuilder clickableHtmlBuilder,
								  final URLSpan urlSpan) {
		int start = clickableHtmlBuilder.getSpanStart(urlSpan);
		int end = clickableHtmlBuilder.getSpanEnd(urlSpan);
		int flags = clickableHtmlBuilder.getSpanFlags(urlSpan);
		ClickableSpan clickableSpan = new ClickableSpan() {
			public void onClick(View view) {
//				//Do something with URL here.
//				Log.d(TAG,"xxxxxxxxxxxxxxxxxxxxxxxxxx");
				Intent it = new Intent(Intent.ACTION_VIEW, Uri.parse(urlSpan.getURL()));
				it.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
				try {
					context.startActivity(it);
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
		};
		clickableHtmlBuilder.setSpan(clickableSpan, start, end, flags);
	}
	private CharSequence getClickableHtml(String html) {
		Spanned spannedHtml = Html.fromHtml(html);
		SpannableStringBuilder clickableHtmlBuilder = new SpannableStringBuilder(spannedHtml);
		URLSpan[] urls = clickableHtmlBuilder.getSpans(0, spannedHtml.length(), URLSpan.class);
		for(final URLSpan span : urls) {
//			Log.d(TAG,"url : "+span.getURL());
			setLinkClickable(clickableHtmlBuilder, span);
		}
		return clickableHtmlBuilder;
	}
class Holder {
	TextView title_text,content_text;
}
}
